package freebase;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.util.Map.*;

public class HandleTSV2Name {

	static Connection conn;
	static Statement stmt;

	public static void main(String[] args) throws Exception {
		openconn();
		String tsvfilelist = args[0];
		String dir = args[1];
		BufferedReader br_files = new BufferedReader(
				new FileReader(tsvfilelist));
//		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
//				args[2]), "utf-8");
		HashMap<String, String> guid_name = new HashMap<String, String>();
		String line = "";
		int filen = 0;
		while ((line = br_files.readLine()) != null) {
			filen++;
			System.out.println("filen " + filen + "\t" + line);
			String file = line.replace("./", dir + "/");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line2 = br.readLine();// this is metadata
			while ((line2 = br.readLine()) != null) {
				String[] abc = line2.split("\t");
				String guid = abc[1].replace("/guid/", "");
				String name = abc[0].replaceAll("'","''");
				if(name.length()>=1){
					insertPair(guid,name);
				}
			}
			br.close();

		}
//		Iterator<Entry<String, String>> it = guid_name.entrySet().iterator();
//		while (it.hasNext()) {
//			Entry<String, String> e = it.next();
//			osw.write(e.getKey() + "\t" + e.getValue() + "\n");
//		}
//		osw.close();
		br_files.close();
	}

	public static void openconn() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(
					"jdbc:mysql://pardosa06.cs.washington.edu/freebase",
					"clzhang", "wiki");
			stmt = conn.createStatement();
		} catch (Exception ex) {
			System.out.println("Error : " + ex.toString());
		}
	}

	public static void insertPair(String guid, String name) {
		try {
			String query = "select guid from guid_name where guid='"+ guid+"'";
			ResultSet res = stmt
					.executeQuery(query);
			if(!res.next()){
				stmt.executeUpdate("insert into guid_name values ('"+guid+"','"+name+"')");				
			}
			
		} catch (Exception ex) {
			System.out.println("Error : " + ex.toString());
		}

	}

	public static void main2(String[] args) {
		try {

			// ResultSet res;

			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(
					"jdbc:mysql://pardosa06.cs.washington.edu/freebase",
					"clzhang", "wiki");

			stmt = conn.createStatement();

			// res = stmt
			// .executeQuery("select MAX(wpid) AS MAXID from articlesMeta");
			// int maxid = -1;
			// while (res.next()) {
			// String maxidstr = res.getString("MAXID");
			// if(maxidstr!=null){
			// maxid = Integer.parseInt(maxidstr);
			// }
			// }
			// res.close();

			BufferedReader br = new BufferedReader(new FileReader(args[0]));
			String line = "";
			int ln = 1;
			int inserted = 0;
			while ((line = br.readLine()) != null) {
				// if (ln <= maxid) {
				// ln++;
				// continue;
				// }
				try {
					// line = line.replaceAll("'","''");
					String[] abc = line.split("\t");
					String wpid = abc[0];
					String name = abc[1].replaceAll("'", "''");
					ResultSet res = stmt
							.executeQuery("select wpid from articlesMeta where wpid="
									+ wpid);
					if (res.next()) {
						continue;
					}
					String query = "insert into articlesMeta values (";
					query += wpid + "," + "'" + name + "')";
					stmt.executeUpdate(query);
					ln++;
					inserted++;
					if (inserted % 100000 == 0)
						System.out.print(".");
					if (inserted % 10000000 == 0)
						System.out.println();
				} catch (Exception e) {
					System.err.println(e.toString());
				}
			}

			br.close();
		} catch (Exception ex) {
			System.out.println("Error : " + ex.toString());
		}

	}
}
